import re
from curl_cffi import requests
from util.headers import get_header
from util.logger import get_looger
from util.filessave import save_data

# root = save_data("彼岸")
url = "https://pic.netbian.com/e/search/result/index.php?page=0&searchid=1224"
res = requests.get(url, headers=get_header())
html_data = res.content.decode("gbk")
img_datas = re.findall(
    r'<li>.*?<a href="/tupian/(.*?)" target="_blank">.*?<img src="/uploads/allimg/(.*?)" alt=".*?<b>(.*?)</b></a>.*?</li>',
    html_data,
    re.S,
)
# print(img_datas)
# img_title = re.findall(r"")

for data in img_datas:
    detail_url = f"https://pic.netbian.com/tupian/{data[0]}"
    thumb_url = f"https://pic.netbian.com/uploads/allimg/{data[1]}"
    title = data[2]
    title_head = title.split("<", 1)[0]
    thumb_data = requests.get(
        thumb_url,
        headers=get_header(
            "https://pic.netbian.com/e/search/result/index.php?page=0&searchid=1224"
        ),
    ).content
    # print(data)
    # print(thumb_data)
    with open(f"{root}{title_head}_thumb.jpg", "wb") as f:
        f.write(thumb_data)
        print(f"爬取{title_head}的照片完毕")
